package org.dromara.quick.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.IdGenerator;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.quick.chat.domain.ImSocialAlbum;
import org.dromara.quick.chat.mapper.ImSocialAlbumMapper;
import org.dromara.quick.chat.model.bo.create.ImSocialAlbumBO;
import org.dromara.quick.chat.model.vo.ImSocialAlbumVO;
import org.dromara.quick.chat.service.ImSocialAlbumService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Stars
* @description 针对表【im_social_album(社交软件中的相册表，用于存储用户相册里的图片信息)】的数据库操作Service实现
* @createDate 2025-03-04 22:33:21
*/
@Service
@RequiredArgsConstructor
public class ImSocialAlbumServiceImpl extends ServiceImpl<ImSocialAlbumMapper, ImSocialAlbum>
    implements ImSocialAlbumService{

    private final ImSocialAlbumMapper imSocialAlbumMapper;

    @Override
    public List<ImSocialAlbumVO> listImSocialAlbum() {
        Long userId = LoginHelper.getUserId();
        return imSocialAlbumMapper.selectVoList(new LambdaQueryWrapper<ImSocialAlbum>().eq(ImSocialAlbum::getUserId, userId).orderByAsc(ImSocialAlbum::getImageOrder));
    }

    @Override
    public void addImSocialAlbum(List<ImSocialAlbumBO> imSocialAlbumList) {
        Long userId = LoginHelper.getUserId();

        Date date = new Date();
        List<ImSocialAlbum> albumList = new ArrayList<>(imSocialAlbumList.size());
        for (ImSocialAlbumBO imSocialAlbumBO : imSocialAlbumList) {
            ImSocialAlbum imSocialAlbum = new ImSocialAlbum();
            imSocialAlbum.setId(IdGenerator.getId());
            imSocialAlbum.setUserId(userId);
            imSocialAlbum.setImageOrder(imSocialAlbumBO.getImageOrder());
            imSocialAlbum.setImageUrl(imSocialAlbumBO.getImageUrl());
            imSocialAlbum.setCreateTime(date);
            albumList.add(imSocialAlbum);
        }
        imSocialAlbumMapper.insertBatch(albumList);
    }

    @Override
    public void updateImSocialAlbum(List<ImSocialAlbumBO> imSocialAlbumList) {
        Long userId = LoginHelper.getUserId();
        List<ImSocialAlbum> albumList = imSocialAlbumMapper.selectList(new LambdaQueryWrapper<ImSocialAlbum>().eq(ImSocialAlbum::getUserId, userId));
        Map<String, ImSocialAlbumBO> socialAlbumBOMap = imSocialAlbumList.stream().collect(Collectors.toMap(ImSocialAlbumBO::getImageUrl, imSocialAlbumBO -> imSocialAlbumBO));
        for (ImSocialAlbum imSocialAlbum : albumList) {
            ImSocialAlbumBO imSocialAlbumBO = socialAlbumBOMap.get(imSocialAlbum.getImageUrl());
            if (imSocialAlbumBO != null) {
                imSocialAlbum.setImageOrder(imSocialAlbumBO.getImageOrder());
            }
        }
        imSocialAlbumMapper.updateBatchById(albumList);
    }

    @Override
    public void deleteImSocialAlbum(Long id) {
        Long userId = LoginHelper.getUserId();
        imSocialAlbumMapper.delete(new LambdaQueryWrapper<ImSocialAlbum>().eq(ImSocialAlbum::getUserId, userId).eq(ImSocialAlbum::getId, id));
    }
}




